<template>
  <div class="detail">
    <el-card>
      <el-tabs>
        <el-tab-pane label="登录帐户设置">
          <el-form ref="form" label-width="100px" :model="form" :rules="rules">
            <el-form-item label="姓名" prop="username">
              <el-input v-model="form.username" />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="form.password" type="password" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submit">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情">
          <info :user-info="userInfo" @updateUserInfo="updateUserInfo" />
        </el-tab-pane>
        <el-tab-pane label="岗位信息">
          <job />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>
<script>
import { sysUser } from '@/api/user.js'
import { sysUserPut } from '@/api/employees.js'
// import info from './components/info.vue' 使用懒加载
export default {
  components: {
    info: () => import('./components/info.vue'),
    job: () => import('./components/job.vue')
  },
  data() {
    return {
      id: this.$route.params.id,
      form: {
        username: '', //	string	非必须
        password: '' //	string	非必须
      },
      userInfo: {},
      rules: {
        username: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          }
        ], //	string	非必须
        password: [
          {
            required: true,
            message: '必填',
            trigger: 'change'
          },
          {
            min: 6,
            max: 12,
            message: '请输入6-12位密码',
            trigger: 'change'
          }
        ] //	string	非必须
      }
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysUser(this.id)
      //   this.form = res.data
      this.userInfo = res.data
      this.form.username = res.data.username
      console.log('用户信息', res)
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await sysUserPut({ ...this.userInfo, ...this.form })
          this.$message.success('修改成功')
        }
      })
    },
    // 子组件修改了用于更新父组件的用户信息
    updateUserInfo(info) {
      this.form.username = info.username
      this.userInfo = JSON.parse(JSON.stringify(info))
    }
  }
}
</script>
<style lang="scss" scoped>
.detail {
  padding: 20px;
}
</style>
